本文结合实例给出如何将AWS EC2 Public DNS绑定到CNAME,其实直接修改A记录就可以了。

1. 直接修改A记录

最开始看别人博文的时候,有几处说不能直接将Elastic IP解析到A记录,然后就摸索着如何解析到CNAME,最后我也确实搞定了(用我另一个域名先调试好,tobediff.com),见本文第二部分。但奇怪的是,当我用本域名sparkandshine.net重新设置时,老是提示这样的错误(我到现在都没搞懂,求指点):

This webpage has a redirect loop 
ERR_TOO_MANY_REDIRECTS

一直很纳闷,为什么不能直接用A记录定向到Elastic IP,收费没道理呀。今晚回来又研究了下,查了下AWS文档关于Elastic IPs收费部分Elastic IP Addresses,如下:

You can have one Elastic IP (EIP) address associated with a running instance at no charge. If you associate additional EIPs with that instance, you will be charged for each additional EIP associated with that instance per hour on a pro rata basis. Additional EIPs are only available in Amazon VPC.

To ensure efficient use of Elastic IP addresses, we impose a small hourly charge when these IP addresses are not associated with a running instance or when they are associated with a stopped instance or unattached network interface.

可见,EIP只要关联到正在运行实例就不收费,收费情况已用红色标出。这么一来,绑定域名就很简单了,直接修改A记录:@ --> xx.xx.xx.xx (Elastic IP)。

PS:如果访问域名提示“This website is temporarily unavailable, please try again later.”,不用着急,再等等就可以了。

2. 通过CNAME绑定

网上多处说,如果将Elastic IPs(只能绑定在EC2 Instance)直接映射到A记录,将会收费,所以映射到CNAME记录。看了多篇博文,最后在这里找到了详细的步骤。以我为例:

  • 域名(在GoDaddy买的):sparkandshine.net
  • AWS Public DNS:ec2-xx-xx-xx-xx.ap-northeast-1.compute.amazonaws.com

2.1 DNS ZONE FILE

(1)修改A记录

如果之前映射到其他主机,修改A记录。注:修改完,IP地址可能会成另一个,没关系,GoDaddy自动指派forwarding IP。

@ --> 50.63.202.12 (GoDaddy's forwarding IP)

(2)修改CNAME的www记录

www --> ec2-xx-xx-xx-xx.ap-northeast-1.compute.amazonaws.com

2.2 SETTINGS

在SETTINGS下的Forwarding:

(1)DOMAIN

Forward to            Redirect      Type
[http://www.sparkandshine.net](http://www.sparkandshine.net/) 301 (Permanent)  Forward only

(2)SUBDOMAIN

Subdomain  Forward to                   Type
www  ec2-xx-xx-xx-xx.ap-northeast-1.compute.amazonaws.com Forward with Masking

3. 讨论

我的理解是,为了避开Elastic IPs直接映射到A记录,多次映射映射绕开这个问题,于是,访问sparkandshine.net就变成这样了:(不确定我理解得对不对)

sparkandshine.net --> 50.63.202.12 (Godaddy域名服务器) --> www.sparkandshine.net --> ec2-xx-xx-xx-xx.ap-northeast-1.compute.amazonaws.com --> xx-xx-xx-xx(amazon域名服务器),达到访问目的

可见多了很多步,如果绑定到A记录,只需要头尾两步。这样是不是速度就慢了许多?

4. A与CNAME区别

关于A与CNAME的区别,可参考博文《A记录和CNAME记录的区别》。以下摘录部分内容:

A记录就是把一个域名解析到一个IP地址(Address,特制数字IP地址),而CNAME记录(别名记录)就是把域名解析到另外一个域名。其功能是差不多,CNAME将几个主机名指向一个别名(注:这个未必吧),其实跟指向IP地址是一样的,因为这个别名也要做一个A记录的。但是使用CNAME记录可以很方便地变更IP地址。如果一台服务器有100个网站,他们都做了别名,该台服务器变更IP时,只需要变更别名的A记录就可以了。

本文系Spark & Shine原创,转载需注明出处本文最近一次修改时间 2022-04-17 10:50

results matching ""

    No results matching ""